var t = require("../../@babel/runtime/helpers/interopRequireDefault")(require("../../@babel/runtime/regenerator")), a = require("../../@babel/runtime/helpers/asyncToGenerator"), e = getApp();

Page({
    data: {
        result: [ "a" ],
        showShare: !1,
        info: [],
        avatar: "",
        posterLogo: "",
        posterName: "",
        qr_code: "",
        posterShow: !1,
        loadingImg: !1,
        logo: "",
        mark_id: "",
        mid: "",
        dataURL: "",
        fontSize: "",
        isclick: !1,
        nums: "",
        share_title: ""
    },
    onLoad: function(t) {
        var a = this, e = wx.getStorageSync("langIndex");
        this.setData({
            language: e.en
        }), t.id && this.setData({
            couponId: t.id
        }), t.info ? (this.setData({
            nums: JSON.parse(t.info).couponNum,
            combine: "false" == JSON.parse(t.info).combine ? 0 : 1
        }), this.init(), this.getdetail(), wx.setNavigationBarTitle({
            title: "优惠券分享"
        }), wx.getSystemInfo({
            success: function(t) {
                var e = t.windowWidth / 375, n = (t.windowWidth, 343 * e), i = 450 * e;
                a.setData({
                    canvasWidth: n,
                    canvasHeight: i
                });
            }
        })) : wx.navigateTo({
            url: "/packageG/index/index"
        });
    },
    init: function() {
        this.setData({
            avatar: "",
            posterLogo: "",
            posterName: "",
            qr_code: "",
            loadingImg: !1,
            posterShow: !1,
            fontSize: 0,
            dataURL: ""
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {
        var t = wx.getStorageSync("yz_uid"), a = wx.getStorageSync("nickname"), e = "";
        return t && (e = t), {
            title: this.data.share_title ? this.data.share_title : "".concat(a, "向您分享").concat(this.data.info.name),
            path: "/packageI/shareGet/shareGet?k=".concat(this.data.mark_id, "&mid=").concat(e)
        };
    },
    onShareTimeline: function() {
        var t = wx.getStorageSync("yz_uid"), a = wx.getStorageSync("nickname"), e = "";
        return t && (e = t), {
            title: this.data.share_title ? this.data.share_title : "".concat(a, "向您分享").concat(this.data.info.name),
            path: "/packageI/shareGet/shareGet?k=".concat(this.data.mark_id, "&mid=").concat(e)
        };
    },
    getShareCoupons: function() {
        var t = this, a = e.getNetAddresss("plugin.share-coupons.Frontend.controllers.share.getShareName");
        e._getNetWork({
            url: a,
            data: {
                coupon_id: this.data.info.id,
                num: this.data.nums
            },
            success: function(a) {
                var e = a.data;
                1 == e.result && (t.data.share_title = e.data.share_title);
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    getdata: function() {
        var t = this, a = {
            record_id: this.data.couponId,
            combine: this.data.combine,
            num: this.data.nums
        }, n = e.getNetAddresss("plugin.share-coupons.Frontend.controllers.list.choose-coupon");
        e._getNetWork({
            url: n,
            data: a,
            success: function(a) {
                var e = a.data;
                1 == e.result && (console.log(e), t.setData({
                    logo: e.data.logo,
                    mark_id: e.data.mark_id
                }));
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    getdetail: function() {
        var t = this, a = e.getNetAddresss("coupon.member-coupon.get-detail");
        a += "&record_id=" + this.data.couponId, e._getNetWork({
            url: a,
            success: function(a) {
                var n = a.data;
                1 == n.result ? (t.setData({
                    info: n.data.coupon
                }), t.data.info.name.length >= 7 ? t.setData({
                    fontSize: "26rpx"
                }) : t.setData({
                    fontSize: "45rpx"
                }), t.getdata(), t.getShareCoupons()) : e.tips(n.msg);
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    getPoster: function() {
        var t = this;
        if (!this.data.isclick) {
            this.data.isclick = !0;
            var a = e.getNetAddresss("plugin.share-coupons.Frontend.controllers.list.poster");
            e._getNetWork({
                url: a,
                data: {
                    mark_id: this.data.mark_id
                },
                success: function(a) {
                    var e = a.data;
                    1 == e.result ? (t.setData({
                        avatar: e.data.avatar,
                        logo: e.data.logo,
                        nickname: e.data.nickname,
                        qr_code: e.data.qr_code,
                        background: e.data.static_img
                    }), t.cavVasMethod()) : t.data.isclick = !1;
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        }
    },
    cavVasMethod: function() {
        var e = this;
        wx.createSelectorQuery().select("#myCanvas").fields({
            node: !0,
            size: !0
        }).exec(function() {
            var n = a(t.default.mark(function a(n) {
                var i, o, s, r, c, d, u, h, l, g, f;
                return t.default.wrap(function(t) {
                    for (;;) switch (t.prev = t.next) {
                      case 0:
                        return wx.showLoading({
                            title: "加载中"
                        }), i = n[0].node, o = i.getContext("2d"), s = n[0].width, r = n[0].height, c = 375 / wx.getSystemInfoSync().windowWidth, 
                        i.width = s * c, i.height = r * c, d = .15 * i.width, t.next = 11, e.downImgae();

                      case 11:
                        return o.restore(), t.next = 14, e.creatImg(e.data.background, o, i, i.width, i.height, 0, 0);

                      case 14:
                        return o.restore(), u = 0 + .05 * i.width, h = i.height - d - .055 * i.width, t.next = 19, 
                        e.creatImg(e.data.avatar, o, i, d, d, u, h);

                      case 19:
                        return o.restore(), o.font = "normal 13px Verdana", o.fillStyle = "#fff", t.next = 24, 
                        setTimeout(function() {
                            e.data.nickname.length > 10 && (e.data.nickname = e.data.nickname.slice(0, 9) + "..."), 
                            o.fillText(e.data.nickname, .4 * d + d, i.height - d), o.fillText("向你分享了优惠券", .4 * d + d, i.height - .5 * d, 110), 
                            o.restore();
                        }, 300);

                      case 24:
                        return l = .35 * i.width, g = i.width - l - .05 * i.width, f = i.height - l - .055 * i.width, 
                        t.next = 29, setTimeout(function() {
                            e.creatImg(e.data.qr_code, o, i, l, l, g, f);
                        }, 400);

                      case 29:
                        return o.restore(), t.next = 32, setTimeout(function() {
                            e.toimage(i, c);
                        }, 1500);

                      case 32:
                      case "end":
                        return t.stop();
                    }
                }, a);
            }));
            return function(t) {
                return n.apply(this, arguments);
            };
        }());
    },
    toimage: function(t, a) {
        var e = this;
        wx.hideLoading(), wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: 580 * a,
            height: 814 * a,
            canvas: t,
            destWidth: this.data.canvasWidth,
            destHeight: this.data.canvasHeight,
            quality: 1,
            success: function(t) {
                var a = t.tempFilePath;
                console.log(t), e.setData({
                    tempFilePath: a,
                    loadingImg: !0
                }), e.data.isclick = !1;
            },
            fail: function(t) {
                e.data.isclick = !1;
            }
        });
    },
    closePopup: function() {
        this.setData({
            loadingImg: !1
        });
    },
    creatImg: function(t, a, e, n, i, o, s) {
        var r = this;
        return new Promise(function(c, d) {
            var u = e.createImage(), h = r;
            u.src = t, u.onload = function() {
                a.drawImage(u, o, s, n, i), c(!0);
            }, u.onerror = function() {
                wx.hideLoading(), h.data.isclick = !1, c(!1);
            };
        });
    },
    downImgae: function() {
        var t = this;
        t.data.background && wx.downloadFile({
            url: this.data.background,
            success: function(a) {
                t.data.background = a.tempFilePath;
            },
            fail: function(a) {
                wx.hideLoading(), t.data.isclick = !1;
            }
        }), this.data.avatar && wx.downloadFile({
            url: this.data.avatar,
            success: function(a) {
                t.data.avatar = a.tempFilePath;
            },
            fail: function(a) {
                wx.hideLoading(), t.data.isclick = !1;
            }
        }), t.data.qr_code && wx.downloadFile({
            url: this.data.qr_code,
            success: function(a) {
                t.data.qr_code = a.tempFilePath;
            },
            fail: function(a) {
                wx.hideLoading(), t.data.isclick = !1;
            }
        });
    }
});